package com.atguigu.yygh.order.controller;


import com.atguigu.yygh.common.handler.JwtHelper;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author xin Zhou
 * @since 2024-02-05
 */
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {


    @Autowired
    private OrderInfoService orderInfoService;

    @PostMapping("/statistics")
    public Map<String,Object> statistics(@RequestBody OrderCountQueryVo orderCountQueryVo){
       return  orderInfoService.statistics(orderCountQueryVo);
    }

    /*
    * 如果请求路径一样,前端区分不出来
    * */
    @ApiOperation("取消订单信息")
    @GetMapping("/cancel/{orderId}")
    public R cancelOrder(@PathVariable Long orderId){
        orderInfoService.cancelOrder(orderId);
        return R.ok();
    }

    @GetMapping("/{orderId}")
    public R detail(@PathVariable Long orderId){
        OrderInfo byId = orderInfoService.detail(orderId);
        return R.ok().data("orderInfo",byId);
    }

    @ApiOperation("获取订单列表")
    @GetMapping("/list")
    public R getOrderList(){
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return R.ok().data("list",statusList);
    }

    @ApiOperation("带条件的分页查询(订单信息)")
    @GetMapping("/{pageNum}/{pageSize}")
    public R getOrderInfoPage(@PathVariable Integer pageNum ,
                              @PathVariable Integer pageSize ,
                              OrderQueryVo orderQueryVo,
                              @RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> page= orderInfoService.getOderInfoPage(pageNum,pageSize,orderQueryVo);
        return R.ok().data("page",page);
    }

    @ApiOperation("提交就诊人的信息")
    @PostMapping("/save/{scheduleId}/{patientId}")
    public R submitOrder(@PathVariable("scheduleId") String scheduleId,
                         @PathVariable("patientId") Long patientId){
       Long orderId = orderInfoService.submitOrder(scheduleId,patientId);
        return R.ok().data("orderId",orderId);
    }

}

